package com.hdu.clj.dncard.util

import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.hdu.clj.dncard.Map1Activity
import com.hdu.clj.dncard.R
import kotlinx.android.synthetic.main.activity_map1.*

class RandomSkill() {

    private val skilllist = arrayOf("card1","card2","card3","card4","card5","card6","card7","card8","card9")
    private val skillattrlist = arrayOf(
        "\n普通攻击\n\n对怪物造成自\n身攻击力x1倍\n的伤害",
        "\n强化攻击\n\n对怪物造成自\n身攻击力x2倍\n的伤害",
        "\n致命一击\n\n对怪物造成自\n身攻击力x3倍\n的伤害",
        "\n普通药水\n\n恢复自身最高\n血量的20%",
        "\n幸运药水\n\n恢复自身最高\n血量的40%",
        "\n救死扶伤水\n\n恢复自身最高\n血量的60%",
        "\n特殊攻击\n\n对怪物造成自\n身智力x2倍\n的伤害",
        "\n魔法攻击\n\n对怪物造成自\n身智力x3倍\n的伤害",
        "\n超强攻击\n\n对怪物造成自\n身智力x5倍\n的伤害"
        )
    private val skillColor = arrayOf("#3AAEE2","#AB30EC","#EC3046","#3AAEE2","#AB30EC","#EC3046","#3AAEE2","#AB30EC","#EC3046")



    fun random():Int{
        val res = (0..100).random()
        return res
    }

    private fun select(num:Int):Int{
        when(num){
            in 0..19 -> return 1
            in 20..29 ->return 2
            in 30..39 ->return 3
            in 40..59 ->return 4
            in 60..69 ->return 5
            in 70..79 ->return 6
            in 80..89 ->return 7
            in 90..95 ->return 8
            else ->return 9
        }
    }

    fun showSkill(context: Context):Array<String>{
        val rand1 = select(random())-1
        val rand2 = select(random())-1
        val rand3 = select(random())-1
        LoadAndSave().save((rand1).toString(),"cardSkill1",context)
        LoadAndSave().save((rand2).toString(),"cardSkill2",context)
        LoadAndSave().save((rand3).toString(),"cardSkill3",context)
        Log.d("curdata","show")



        return arrayOf(skillattrlist[rand1],skillColor[rand1],skillattrlist[rand2],skillColor[rand2],skillattrlist[rand3],skillColor[rand3])
    }

    fun action(act:Int,context: Context):Int{
        var res = 0
//        var choose = ""
        Log.d("curdata",act.toString())
        var choose = ""
        if (act==1){
            choose = LoadAndSave().load("cardSkill1",context)

        }
        else if (act==2){
            choose = LoadAndSave().load("cardSkill2",context)

        }
        else{
            choose = LoadAndSave().load("cardSkill3",context)

        }

        val flag = choose.toInt()/3
        var result = 0
        when(choose){
            "0"->result = LoadAndSave().load("hattack",context).toInt()
            "1"->result = LoadAndSave().load("hattack",context).toInt()*2
            "2"->result = LoadAndSave().load("hattack",context).toInt()*3
            "3"->result = (LoadAndSave().load("maxlife",context).toInt()*0.2).toInt()
            "4"->result = (LoadAndSave().load("maxlife",context).toInt()*0.4).toInt()
            "5"->result = (LoadAndSave().load("maxlife",context).toInt()*0.6).toInt()
            "6"->result = LoadAndSave().load("hwisdom",context).toInt()*2
            "7"->result = LoadAndSave().load("hwisdom",context).toInt()*3
            "8"->result = LoadAndSave().load("hwisdom",context).toInt()*5
        }
        /**
         * @param cur_fight 当前攻击的怪物
         */
        if (flag==0||flag==2){
            res = result*-1
            val cur_fight = LoadAndSave().load("cur_fight",context)
            val m1g1b = LoadAndSave().load(cur_fight+"life",context).toInt()
            if (m1g1b<=result){
                LoadAndSave().save("false",cur_fight,context)
//                when(cur_fight){
//                    "m1g1" -> LoadAndSave().save("m1g2","cur_fight",context)
//                    "m1g2" -> LoadAndSave().save("m1g3","cur_fight",context)
//                }
            }
            else{
                LoadAndSave().save((m1g1b-result).toString(),cur_fight+"life",context)
            }
        }
        else {
            res = result
            val hlife = LoadAndSave().load("hlife",context).toInt()
            val maxHlife = LoadAndSave().load("maxlife",context).toInt()
            if (hlife+result>=maxHlife){
                LoadAndSave().save(maxHlife.toString(),"hlife",context)
            }
            else{
                LoadAndSave().save((hlife+result).toString(),"hlife",context)
                Log.d("cur",(hlife+result).toString())
            }
        }
        return res
    }
    fun mattack(context: Context,lv:Int):Array<Int>{
        var attack = 0
        when(lv){
            1 -> attack = (10..25).random()
            2 -> attack = (20..35).random()
            3 -> attack = (30..50).random()
        }
        val hlife = LoadAndSave().load("hlife",context)
        if (hlife.toInt()<=attack){
            LoadAndSave().save("0","hlife",context)
            return arrayOf(0,attack)
        }
        else{
            LoadAndSave().save((hlife.toInt()-attack).toString(),"hlife",context)
            return arrayOf(1,attack)
        }
    }
    fun finish(context: Context):Int{
        val cur_fight = LoadAndSave().load("cur_fight",context)
        val flag = LoadAndSave().load(cur_fight,context)
        val cur_last = cur_fight.get(3)
        if (cur_last.equals('1')&&flag.equals("false")){
            return 1
        }
        if (cur_last.equals('2')&&flag.equals("false")){
            return 2
        }
        if (cur_last.equals('3')&&flag.equals("false")){
            return 3
        }
        return 0
    }
}